package com.example.module_hp_white_noise.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.Observer;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;

import com.example.module_hp_white_noise.R;
import com.example.module_hp_white_noise.base.BaseMvvmActivity;
import com.example.module_hp_white_noise.base.BaseUtils;
import com.example.module_hp_white_noise.databinding.ActivityHpSleepMonitorBinding;
import com.example.module_hp_white_noise.util.AudioRecordDemo;
import com.example.module_hp_white_noise.viewModel.SleepFocusViewModel;
import com.fwlst.lib_base.utils.AdUtils;
import com.fwlst.lib_base.utils.JtXXPermissions;
import com.hjq.toast.Toaster;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

@Route(path = "/hpWhiteNoise/route/HpSleepMonitorActivity")
public class HpSleepMonitorActivity extends BaseMvvmActivity<ActivityHpSleepMonitorBinding, SleepFocusViewModel> {
    private static final String TAG = "MainActivity";

    private static final int GET_RECODE_AUDIO = 1;
    private static final int refreshTime = 10;
    private static String[] PERMISSION_AUDIO = {
            Manifest.permission.RECORD_AUDIO
    };
    private boolean mIsThreadRun;
    private AudioRecordDemo audioRecordDemo;
    private Thread mDectorthread;
    private InputStream ins;
    @Override
    protected int initLayout() {
        return R.layout.activity_hp_sleep_monitor;
    }

    @Override
    protected int initBR() {
        return BR.data;
    }

    @Override
    protected void initView() {
        BaseUtils.setStatusBar(this,0xff1E2746);
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(this,binding.bannerContainer);
        //verifyAudioPermissions(this);
        audioRecordDemo = new AudioRecordDemo(this);
        //返回按钮
        binding.returnTb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.startBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPermission();
            }
        });
        //监听按钮点击
        viewModel.getCurrentType().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if(integer==0) return;
                switch (integer){
                    case 1://点击了结束
                        audioRecordDemo.sotp();
                        break;
                    case 2://点击了开始
                        audioRecordDemo.getNoiseLevel(new AudioRecordDemo.MonitorInterface() {
                            @Override
                            public void monitorListener(int res) {
                                binding.sleepFocusTimeText.setText(res+"");
                                if(res<=1){
                                    binding.breatheTv.setText("没有声音");
                                }else if(res<=40&&res>1){
                                    binding.breatheTv.setText("比较理想的安静环境");
                                }else if(res<=60&&res>40){
                                    binding.breatheTv.setText("一般普通室内谈话");
                                }else if(res<=70&&res>60){
                                    binding.breatheTv.setText("吵闹、有损神经");
                                }else if(res<=90&&res>70){
                                    binding.breatheTv.setText("很吵、神经细胞受到破坏");
                                }else if(res<=100&&res>90){
                                    binding.breatheTv.setText("吵闹加剧、听力受损");
                                }else if(res>100){
                                    binding.breatheTv.setText("会感到痛苦");
                                }
                            }
                        });
                        break;
                }
            }
        });

    }
    private void getPermission(){
        List<String> pre = new ArrayList<>();
        pre.add(Manifest.permission.RECORD_AUDIO);
        JtXXPermissions.INSTANCE.requestPermission(HpSleepMonitorActivity.this, "", pre, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                AdUtils.INSTANCE.checkFunction(HpSleepMonitorActivity.this, new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        viewModel.setCurrentType(2);
                        return null;
                    }
                });
                return null;
            }
        });

//        PermissionX.init(this)
//                .permissions(Manifest.permission.RECORD_AUDIO)
//                .explainReasonBeforeRequest()
//                .onExplainRequestReason((scope, deniedList, beforeRequest) -> scope.showRequestReasonDialog(deniedList, "需要您同意以下权限才能进行分贝监测", "同意","拒绝"))
//                .request(new RequestCallback() {
//                    @Override
//                    public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
//                        if(allGranted){
//                            MemberUtils.checkFuncEnableV2((Activity) mContext, "hpZaoYin", new MemberUtils.ActionInterface() {
//                                @Override
//                                public void actionListener() {
//
//                                }
//                            });
//                        }else {
//                            Toaster.show("录音权限被禁止或录音已被占用，请开启权限，否则功能无法使用");
//                        }
//                    }
//                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        audioRecordDemo.sotp();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}